<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of order_model
 *
 * @author Administrator
 */
class order_model extends Model{
    //put your code here
    function order_model()
    {
        parent::Model();
        $this->load->database();  //加载数据库
    }

    function preview_order($bookid){
      $sql= "select * from booklist where ";
      $c=count($bookid);
      for($i=0;$i<$c-1;$i++){
          $sql.= " id=$bookid[$i] OR ";
      }
      $sql.= " id={$bookid[$i]} ";
      $query = $this->db->query($sql);
      return $query->result();
    }

    //生成订单
    function gen_order($client,$order_info,$total_money,$deliver_type)
    {
        $data = array(
                    'total_money'=>$total_money,
                    'consignee' => $client['consignee'] ,
                    'user_id'=>$client['user_id'],
                    'address'=>$client['address'],
                    'phone' => $client['phone'] ,
                     'short_phone' =>$client['short_phone'] ,
                     'deliver_type' =>$deliver_type ,
            );
        $this->db->insert('order', $data);    //插入数据
        if(($this->db->affected_rows())!=1){
                return 0;      //如果插入操作影响的行数不为1，则表示插入失败，返回0
        }
        $select = "select max(id) as order_id from `order`";
        $query = $this->db->query($select);
        if( ($result = $query->row_array())!= null )
            $order_id = $result['order_id'];
        foreach($order_info as $order_detail){
            $data2=null;
            $data2=array(
               'order_id'=>$order_id,
               'book_id'=>$order_detail->id,
               'book_title'=>$order_detail->title,
               'author'=>$order_detail->author,
               'edition'=>$order_detail->edition,
               'price'=>$order_detail->price,
            );
            $this->db->insert('order_detail', $data2);    //插入数据
            if(($this->db->affected_rows())!=1){
                return 0;      //如果插入操作影响的行数不为1，则表示插入失败，返回0
            }
            $sql2="UPDATE booklist SET num_stock=num_stock-1 WHERE id='$order_detail->id' ";
            $query=$this->db->query($sql2);
        }
        return $order_id;
    }

    function get_order_toprocess(){
        $select = "select * from `order` where status=0 ORDER BY order_time ASC";  //status 0 represents order to be processed.
        $result = $this->db->query($select);
        return $result->result();
    }

    function get_order_detail($order_id){
        $select = "select * from `order_detail` where order_id=$order_id ";
        $result = $this->db->query($select);
        return $result->result();
    }

    function get_related_order($order_id){
        $select = "select * from `order` where id=$order_id ";
        $query = $this->db->query($select);
        $result=$query->result();
        $user_id=$result[0]->user_id;
        $select2 = "select * from `order` where user_id='$user_id' "; //包括本订单信息，在显示页面再做调整
        $query2 = $this->db->query($select2);
        $result2=$query2->result();
        return $result2;
    }
    
   function update_order_status($order_id,$book_ids,$newstatus){
        $update= "UPDATE `order` SET status=$newstatus where id=$order_id ";
        $result = $this->db->query($update);
        if($newstatus!=0){
          foreach($book_ids  as $book_id){
             if($newstatus==1)
               $update2= "UPDATE `booklist` SET num_sold=num_sold+1 where id=$book_id ";
             if($newstatus==2)
               $update2= "UPDATE `booklist` SET num_sold=num_sold-1 where id=$book_id ";
           $result = $this->db->query($update2);
          }
        }
        return 1;
    }

    function search_order($order_id,$user_id,$consignee){
        $sql="select * from `order` where  ";
        $value1=0;
         if($order_id!=null){
            $sql.=" id=$order_id ";
            $value1=1;
         }
         if($user_id!=null){
            if($value1==1)
            $sql.=" OR user_id LIKE '$user_id%' ";
            else{
                $sql.=" user_id LIKE '$user_id%' ";
                $value1=1;
            }
         }
         if($consignee!=null){
            if($value1==1)
            $sql.=" OR consignee LIKE '$consignee%' ";
            else{
                $sql.=" consignee LIKE '$consignee%' ";
                $value1=1;
            }
         }
         $query = $this->db->query($sql);
        if ($query->num_rows() > 0){
           $result=$query->result() ;
           return $result;
          }
        else
           return null;
    }





    //******返回我的订单
    function get_my_order($user_id)
    {
        $select = "select * from `order` where user_id = '" .$user_id. "'";
        $result = $this->db->query($select);
        return $result->result();
    }


    //加到购物车
    /*function add2shop_car($user_id, $isbn13,$title)
    {
        $select = "select order_number from shop_car where user_id = '" .$user_id. "' and isbn13='" .$isbn13. "'";
        $query = $this->db->query($select);
        if( ($result = $query->row_array())!= null ) {
            $order_number = $result['order_number'] + 1;
            $update = "update shop_car set order_number = " .$order_number. " where user_id = '" .$user_id. "' and isbn13='" .$isbn13. "'";
            $this->db->query($update);
        }
        else{
            $insert = "insert into shop_car(user_id,title,isbn13,order_number) values('" .$user_id. "','" .$title. "','" .$isbn13. "',1)";
            $this->db->query($insert);
        }
    }*/
}
?>
